package com.hnkywl.sts.service.cggl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.cggl.CgdDao;
import com.hnkywl.sts.dao.cggl.CgdmxDao;
import com.hnkywl.sts.dao.cggl.GlfyDao;
import com.hnkywl.sts.dao.jhgl.BgjhhzmxDao;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import com.hnkywl.sts.entity.cggl.Glfy;
import com.hnkywl.sts.entity.jhgl.Bgjhhzmx;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午11:10
 * To change this template use File | Settings | File Templates.
 */
@Service
public class BgCgdService {

    // 采购单dao
    private CgdDao cgdDao;
    private BmService bmService;
    private GlfyDao glfyDao;
    private BgjhhzmxDao bgjhhzmxDao;
    private CgdmxDao cgdmxDao;


    /**
     * 查询保供采购单
     *
     * @param cgdh
     * @param fhrqStart
     * @param fhrqEnd
     * @param cglx
     * @param cggsId
     * @param cgbmId
     * @param cgrId
     * @param zt
     * @param sftzd
     * @param page
     * @param rows
     * @return
     * @throws ParseException
     */
    public String findAllBgCgdList(Integer sfgq, String wzbm, String cgdh, Long dzckId, Date fhrqStart, Date fhrqEnd, String cglx, Long cggsId, Long cgbmId, Long cgrId, String zt, String sftzd, String htType, Long gysid, String rkzt, int page, int rows) throws ParseException {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page bgCgdPage = this.cgdDao.findAllBgCgdList(sfgq, wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, null, page, rows);
        List<Map> sumList = cgdDao.findSumBgCgd(sfgq, wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, null);
        String cgdmxJson = gson.toJson(bgCgdPage.getResult());
        Double cgsl = 0d;
        Double hse = 0d;
        Double gylfyje = 0d;
        if (sumList.size() != 0) {
            for (Map map : sumList) {
                cgsl += Double.valueOf((map.get("cgsl") == null ? 0d : map.get("cgsl")).toString());
                hse += Double.valueOf((map.get("cgje") == null ? 0d : map.get("cgje")).toString());
                //gylfyje += Double.valueOf((map.get("gylfyje") == null ? 0d : map.get("gylfyje")).toString());
            }
            return "{\"total\":" + bgCgdPage.getTotalCount() + ",\"rows\":" + cgdmxJson + ",\"footer\":[{\"cgdh\":\"合计:\",\"cgsl\":" + cgsl + ",\"cgje\":" + hse + "}]}";
        } else {
            return "{\"total\":" + bgCgdPage.getTotalCount() + ",\"rows\":" + cgdmxJson + "}";
        }
    }

    /**
     * 首页显示保供采购单
     *
     * @return
     */
    public String findAllBgcgd() {
        Integer bgcgdNum = 0;
        try {
            bgcgdNum = this.cgdDao.findAllBgCgdList(null, null, null, null, null, null, null, null, null, null, "ybc", "false", "bght", null, null, null, 0, -1).getTotalCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bgcgdNum.toString();
    }

    //保供采购单到货日期延期
    @Transactional
    public void saveDhyq(Cgd cgd) {
        Cgd clone = cgdDao.get(cgd.getId());
        clone.setJhdhrq(cgd.getJhdhrq());
        clone.setLastUpdated(new Date());
        this.cgdDao.save(clone);
    }

    @Transactional
    public void addBgCgd(Cgd cgd, String hzmxIds) {
        initBgCgd(cgd);

        cgd.setZt("ybc");
        if (cgd.getSl() != null) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.0000");
            String sl = df.format(Double.parseDouble(cgd.getSl()));
            cgd.setSl(sl);
        }
        cgd.setCreateUser(YgService.getCurrentYg());
        cgd.setCreateOrgan(cgd.getSbgs());
        cgd.setCreateDepartment(cgd.getSbbm());
        cgd.setLastUpdated(cgd.getDateCreated());
        String[] hzmxids = hzmxIds.split(",");


        if (cgd.getCgdmxList() != null && cgd.getCgdmxList().size() > 0) {
            for (Cgdmx cgdmx : cgd.getCgdmxList()) {
                List<Bgjhhzmx> bgjhhzmxes = new ArrayList<Bgjhhzmx>();
                for (int i = 0; i < hzmxids.length; i++) {
                    Bgjhhzmx bgjhhzmx = bgjhhzmxDao.get(Long.parseLong(hzmxids[i]));
                    if (bgjhhzmx.getWzgg().getId().equals(cgdmx.getGg().getId())) {
                        if (this.cgdDao.calcCgdmxSbsl(bgjhhzmx.getId(), cgdmx) >= bgjhhzmx.getJhsl())
                            bgjhhzmx.setZt(8);
                        else
                            bgjhhzmx.setZt(7);
                        bgjhhzmx.setLastUpdated(new Date());
                        bgjhhzmxes.add(bgjhhzmx);
                        bgjhhzmxDao.save(bgjhhzmx);

                        cgdmx.setCreateUser(YgService.getCurrentYg());
                        cgdmx.setCreateOrgan(cgd.getSbgs());
                        cgdmx.setCreateDepartment(cgd.getSbbm());
                        cgdmx.setDateCreated(new Date());
                        cgdmx.setLastUpdated(cgdmx.getDateCreated());
                        cgdmx.setBgjhhzmxList(bgjhhzmxes);
                        break;
                    }
                }
            }
        }
        if (cgd.getCgfyList() != null && cgd.getCgfyList().size() != 0) {
            for (Glfy glfy : cgd.getCgfyList()) {
                glfy.setDateCreated(new Date());
                glfy.setLastUpdated(new Date());
                glfy.setCreateUser(YgService.getCurrentYg());
                glfy.setCreateOrgan(cgd.getSbgs());
                glfy.setCreateDepartment(cgd.getSbbm());
                glfyDao.save(glfy);
            }
        }
        this.cgdDao.save(cgd);
    }

    @Transactional
    public void updateBgCgd(Cgd cgd, String hzmxIds) {
        Cgd temp = this.cgdDao.get(cgd.getId());
        temp = (Cgd) ReflectionUtils.getInstance(temp, cgd);
        String[] hzmxids = hzmxIds.split(",");
        if (temp.getCgdmxList() != null && temp.getCgdmxList().size() > 0) {
            for (Cgdmx cgdmx : temp.getCgdmxList()) {
                List<Bgjhhzmx> bgjhhzmxes = new ArrayList<Bgjhhzmx>();
                for (int i = 0; i < hzmxids.length; i++) {
                    if (StringUtils.isNotEmpty(hzmxids[i])) {
                        Bgjhhzmx bgjhhzmx = bgjhhzmxDao.get(Long.parseLong(hzmxids[i]));
                        if (bgjhhzmx.getWzgg().getId().equals(cgdmx.getGg().getId())) {

                            bgjhhzmx.setLastUpdated(new Date());
                            bgjhhzmxes.add(bgjhhzmx);
                            bgjhhzmxDao.save(bgjhhzmx);

                            cgdmx.setLastUpdated(cgdmx.getDateCreated());
                            cgdmx.setBgjhhzmxList(bgjhhzmxes);


                        }
                    }
                }
            }
        }
        initBgCgd(temp);

        if (temp.getCgfyList() != null && temp.getCgfyList().size() != 0) {
            for (Glfy glfy : temp.getCgfyList()) {
                glfy.setDateCreated(new Date());
                glfy.setLastUpdated(glfy.getDateCreated());
                glfy.setCreateUser(YgService.getCurrentYg());
                glfy.setCreateOrgan(temp.getSbgs());
                glfy.setCreateDepartment(temp.getSbbm());
                glfyDao.save(glfy);
            }
        }

        temp.setLastUpdated(new Date());
        this.cgdDao.save(temp);

//        if (temp.getCgdmxList() != null) {
//            for (Cgdmx cgdmx : temp.getCgdmxList()) {
//                if (cgdmx.getBgjhhzmxList() != null) {
//                    for (Bgjhhzmx bgjhhzmx : cgdmx.getBgjhhzmxList()) {
//                        if (this.cgdDao.calcCgdmxSbsl(bgjhhzmx.getId(), cgdmx) >= bgjhhzmx.getJhsl())
//                            bgjhhzmx.setZt(8);
//                        else
//                            bgjhhzmx.setZt(7);
//                        break;
//                    }
//                }
//            }
//        }
    }

    private void initBgCgd(Cgd cgd) {
        if (cgd.getLj() != null && cgd.getLj().getId() == null) {
            cgd.setLj(null);
        }
        if (cgd.getCght() != null && cgd.getCght().getId() == null) {
            cgd.setCght(null);
        }
        if (cgd.getGys() != null && cgd.getGys().getId() == null) {
            cgd.setGys(null);
        }
        if (cgd.getDzck() != null && cgd.getDzck().getId() == null) {
            cgd.setDzck(null);
        }

        if (cgd.getYsdw() != null && cgd.getYsdw().getId() == null) {
            cgd.setYsdw(null);
        }

        if (cgd.getCggs() != null && cgd.getCggs().getId() == null) {
            cgd.setCggs(null);
        }
        if (cgd.getCgbm() != null && cgd.getCgbm().getId() == null) {
            cgd.setCgbm(null);
        }
        if (cgd.getCgr() != null && cgd.getCgr().getId() == null) {
            cgd.setCgr(null);
        }
        if (cgd.getSbgs() != null && cgd.getSbgs().getId() == null) {
            cgd.setSbgs(null);
        }
        if (cgd.getSbbm() != null && cgd.getSbbm().getId() == null) {
            cgd.setSbbm(null);
        }
        if (cgd.getSbr() != null && cgd.getSbr().getId() == null) {
            cgd.setSbr(null);
        }
        if (cgd.getFhgs() != null && cgd.getFhgs().getId() == null) {
            cgd.setFhgs(null);
        }

        if (cgd.getCgdmxList() != null) {
            for (Cgdmx cgdmx : cgd.getCgdmxList()) {
                if (cgdmx.getJzfs() != null && cgdmx.getJzfs().getId() == null) {
                    cgdmx.setJzfs(null);
                }
                if (cgdmx.getCd() != null && cgdmx.getCd().getId() == null) {
                    cgdmx.setCd(null);
                }
            }
        }

        if (cgd.getCgdh() == null || "".equals(cgd.getCgdh())) {
            cgd.setCgdh(this.bmService.getCgdbm(cgd.getCglx()));
        }
    }

    @Transactional
    public void deleteBgCgd(Cgd cgd) {
        cgd = this.cgdDao.get(cgd.getId());
        if (cgd.getCgjsd() != null) {
            throw new RuntimeException("禁止删除！该采购单已结算");
        }
        cgd.setLastUpdated(new Date());
        cgd.setDeleted(1);
        this.cgdDao.save(cgd);
        List<Cgdmx> cgdmxes = cgd.getCgdmxList();
        for (Cgdmx cgdmx : cgdmxes) {
            List<Bgjhhzmx> bgjhhzmxes = cgdmx.getBgjhhzmxList();
            for (Bgjhhzmx bgjhhzmx : bgjhhzmxes) {
                bgjhhzmx.setZt(7);
                bgjhhzmx.setLastUpdated(new Date());
                bgjhhzmxDao.save(bgjhhzmx);
            }
            cgdmx.setBgjhhzmxList(null);
            cgdmx.setDeleted(1);
        }
        if (cgd.getCgdmxList() != null && cgd.getCgdmxList().size() != 0) {
            for (Cgdmx cgdmx : cgd.getCgdmxList()) {
                cgdmx.setDeleted(1);
                cgdmx.setLastUpdated(new Date());
                cgdmxDao.save(cgdmx);
            }
        }
        if (cgd.getCgfyList() != null && cgd.getCgfyList().size() != 0) {
            for (Glfy glfy : cgd.getCgfyList()) {
                glfy.setDeleted(1);
                glfy.setLastUpdated(new Date());
                glfyDao.save(glfy);
            }
        }
    }

    @Autowired
    public void setCgdDao(CgdDao cgdDao) {
        this.cgdDao = cgdDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setGlfyDao(GlfyDao glfyDao) {
        this.glfyDao = glfyDao;
    }

    @Autowired
    public void setBgjhhzmxDao(BgjhhzmxDao bgjhhzmxDao) {
        this.bgjhhzmxDao = bgjhhzmxDao;
    }

    @Autowired
    public void setCgdmxDao(CgdmxDao cgdmxDao) {
        this.cgdmxDao = cgdmxDao;
    }
}
